<?php
namespace app\common\model;
use traits\model\SoftDelete;

class Admin extends Base
{
    use SoftDelete;
    protected $deleteTime = 'delete_time'; // 软删除字段
    protected $autoWriteTimestamp = true;

    /**
     * 校对账户密码
     * @param string $phone
     * @param string $password
     * @return int 成功返回uid 禁用0 密码错误-1
     */
    public function checkPassword($phone,$password)
    {
        $info = $this->getArrayByMap(['phone'=>$phone],'','',false);
        if(empty($info)){
            return -2;
        }else{
            if(isset($info['status']) && $info['status'] == 2){
                return 0;
            }else{
                if(md5($password.$info['salt']) != $info['password']){
                    return -1;
                }else{
                    return $info['id'];
                }
            }
        }
    }

    /**
     * 根据id连表查询管理员信息
     * @param int $id
     * @return array
     */
    public function access()
    {
        return $this->hasOne('AuthGroupAccess','uid','id');
    }


    public static function getAdminByID($id)
    {
        $admin = self::with('access')->find($id); // 通过 with 使用关联模型，参数为关联关系的方法名
        return $admin;
    }

    public static function getAdminsByMap($map)
    {
//        $admins = self::with(['access,access.group'])->where($map)->order('id desc')->paginate();
        $admins = self::field('a.*,b.group_id,c.title')
            ->alias('a')
            ->join('__AUTH_GROUP_ACCESS__ b','b.uid=a.id','LEFT')
            ->join('__AUTH_GROUP__ c','c.id=b.group_id','LEFT')
            ->where($map)
            ->order('id desc')
            ->paginate();
        return $admins;
    }

}